-- Group-o-matic 0.1a
-- Dev: Kostas Karachalios
-- eMail: group-o-matic@googlegroups.com
-- URL: http://code.google.com/p/group-o-matic/
 
local instance				-- to be made a pull-down menu
local levelAdj = 0			-- to be made a slider
local pugTactic				-- to be made a pull-down menu or list with + and - buttons
local lfg = true			-- to be made a checkbox
local lfgChan = 4			-- automaticly selected
local UpdateInterval = 10.0	-- this should be the LFG channel "wait-time" in seconds, will be made a slider
local TimeSinceLastUpdate = 0
local auto = false			-- default

-- supportive functions
local function MyAddon_GetCmd(msg)		-- thx to Tigerheart
	if msg then
		local a,b,c=strfind(msg, "(%S+)"); --contiguous string of non-space characters
		if a then
			return c, strsub(msg, b+2);
		else	
			return "";
		end
	end
end

local function MyAddon_GetArgument(msg)	-- thx again to Tigerheart
	if msg then
		local a,b=strfind(msg, "=");
		if a then
			return strsub(msg,1,a-1), strsub(msg, b+1);
		else	
			return "";
		end
	end
end

local function MsgToTable( msg )	-- thx to Aquendyn
	if not msg then return end
	local t = {};
	gsub( msg, "%S+", function(w)
		tinsert( t, w )
	end)
	return t;
end

local function writeLine(s)	-- thx to ChrisHill (i think)
	if DEFAULT_CHAT_FRAME then
		DEFAULT_CHAT_FRAME:AddMessage(s)
	end
end


--------------------
-- INITIALIZATION --
--------------------
function Gom_OnLoad()

	-- slash commmands
	SlashCmdList["GROUPOMATIC"] = Gom_Parse;
	SLASH_GROUPOMATIC1 = "/gom";
	SLASH_GROUPOMATIC2 = "/groupomatic";
	
	writeLine("Group-o-matic 0.1a loaded");

end

-- supportive functions
local function MsgToTable( msg )	-- thx to Aquendyn
	if not msg then return end
	local t = {};
	gsub( msg, "%S+", function(w)
		tinsert( t, w )
	end)
	return t;
end

local function writeLine(s)	-- thx to ChrisHill (i think)
	if DEFAULT_CHAT_FRAME then
		DEFAULT_CHAT_FRAME:AddMessage(s)
	end
end

-- calling functions
local function Lfg(instance)
	
	-- finding the channel ID
	if channelId == nil then
		rot = 1
		while name ~= nil do
			channelId, channel = GetChannelName(rot);
			if channel == "LookingForGroup" then break end
			rot = rot + 1
		end
	end
	
	-- finding level and class
	if playerClass == nil then
		playerClass = UnitClass("player")
	end
	playerLvl = UnitLevel("player")
	
	-- building the message
	msg = playerLvl .." ".. playerClass .." LFG ".. instance
	
	-- send the message
	SendChatMessage(msg, "CHANNEL", "COMMON", channelId)
--	writeLine(msg)
end

-- main functions
function Gom_Parse(msg)
	
	Gom_ArgTable = MsgToTable(msg)
	
	if Gom_ArgTable[1] == "lfg" then
		if Gom_ArgTable[2] then
			if auto == false then
				Lfg(Gom_ArgTable[2])
			end
			if selInstance then
				writeLine("Instance changed to ".. Gom_ArgTable[2])
			end
			selInstance = Gom_ArgTable[2]
		else
			writeLine("Please add an instance abbreviation to your command")
		end
	elseif Gom_ArgTable[1] == "auto" then
		if Gom_ArgTable[2] == "on" then
			if selInstance == nil then
				writeLine("Please specify first your prefered instance with /gom lfg <instance>")
			else
				auto = true
				writeLine("GOM: auto LFG posting on for ".. selInstance)
			end
		else
			auto = false
			writeLine("GOM: auto LFG posting off")
		end
	end

end

-- update functions
function Gom_OnUpdate(elapsed)
	
	if auto then
		TimeSinceLastUpdate = TimeSinceLastUpdate + elapsed;
	
		if (TimeSinceLastUpdate > UpdateInterval) then
			Lfg(selInstance)
		
		TimeSinceLastUpdate = 0;
		end
	end
end
